package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.publish.domain.RecommendArticle;
import com.ruoyi.publish.service.RecommendArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/publish/recommendArticle")
public class RecommendArticleController extends BaseController {
    @Autowired
    private RecommendArticleService recommendArticleService;
    @PostMapping("/add")
    public AjaxResult addArticle(@RequestBody RecommendArticle recommendArticle){
        System.out.println("新增推荐文章的recommendId：" + recommendArticle.getRecommendId());
        recommendArticle.setCreateBy(SecurityUtils.getUsername());
        int result=recommendArticleService.insert(recommendArticle);
        return toAjax(result);
    }


    //查询所有文章的方法
    @GetMapping("/listAll")
    public AjaxResult selectAllArticles(  @RequestParam(value = "columnIdList", required = false) List<Long> columnIdList,
                                          @RequestParam(value = "excludeArticleId", required = false) List<Long> excludeArticleId,
                                          @RequestParam(value = "startDate", required = false) String startDate,
                                          @RequestParam(value = "endDate", required = false) String endDate,
                                          @RequestParam(value = "excludeRecommended", defaultValue = "false") boolean excludeRecommended ) {

        return AjaxResult.success(recommendArticleService.selectAllArticles(columnIdList,excludeArticleId,startDate,endDate,excludeRecommended));
    }

    @GetMapping("/get")
    public AjaxResult selectArticleByRecommend(@RequestParam(value = "recommendArticleId" ,required = false) Long recommendArticleId){

        return AjaxResult.success(recommendArticleService.select(recommendArticleId));

    }

    @DeleteMapping("/{recommendArticleId}")
    public AjaxResult deleteArticle(@PathVariable Long recommendArticleId){
        return toAjax(recommendArticleService.delete(recommendArticleId));
    }
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody RecommendArticle recommendArticle){
        recommendArticle.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(recommendArticleService.update(recommendArticle));
    }


}
